package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.StringUtil;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.entity.User;
import org.easy.tool.mapper.UserMapper;
import org.easy.tool.service.IUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Valley
 * @since 2023-08-25 14:20:43
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUser {

    @Autowired
    UserMapper userMapper;

    @Override
    public User getUserByName(String userName) {
        LambdaQueryWrapper<User> lambda = new LambdaQueryWrapper<>();
        lambda.eq(User::getName, userName);
        return getOne(lambda);
    }

    @Override
    public User getUserById(String userId) {
        LambdaQueryWrapper<User> lambda = new LambdaQueryWrapper<>();
        lambda.eq(User::getId, userId);
        return getOne(lambda);
    }

    @Override
    public User getUserByPhone(String phone) {
        LambdaQueryWrapper<User> lambda = new LambdaQueryWrapper<>();
        lambda.eq(User::getPhone, phone);
        return getOne(lambda);
    }

    @Override
    public String addUser(User user) {
        user.setCreateTime(TimeUtil.now());
        user.setUpdateTime(TimeUtil.now());
        save(user);
        return user.getId();
    }

    @Override
    public User getUser(User user) {
        if (StringUtil.isNotBlank(user.getId())){
            return getUserById(user.getId());
        } else if (StringUtil.isNotBlank(user.getName())){
            return getUserByName(user.getName());
        } else if (StringUtil.isNotBlank(user.getPhone())){
            return getUserByPhone(user.getPhone());
        }
        return null;
    }


}
